﻿/*
=======================================================
    InstallAddress【安装地点】
=======================================================
*/

namespace Services
{
    public interface IInstallAddressMapping : IMapping<InstallAddress, InstallAddressModel, InstallAddressFilterModel>
    { }

    public class InstallAddressMapping : AutoInstallAddressMapping, IInstallAddressMapping
    {
        private readonly ILoginUserDataPermission _ILoginUserDataPermission;

        public InstallAddressMapping(ICurrentUser currentUser, ILoginUserDataPermission iLoginUserDataPermission)
            : base(currentUser)
        {
            _ILoginUserDataPermission = iLoginUserDataPermission;
        }

        #region 读数据
        /// <summary>
        /// 读数据，从Entity对象转成Model对象
        /// </summary>
        /// <param name="entity">Entity对象</param>
        /// <returns></returns>
        public override InstallAddressModel Entity2Model(InstallAddress entity)
        {
            if (entity == null)
            {
                return null;
            }

            InstallAddressModel model = base.Entity2Model(entity);


            /***************************************** 下面处理自定义转换 *****************************************/



            return model;
        }
        #endregion

        #region 写数据
        /// <summary>
        /// 写数据，从Model对象转换成Entity对象
        /// </summary>
        /// <param name="model">ViewModel对象</param>
        /// <returns></returns>
        public override InstallAddress Model2Entity(InstallAddressModel model)
        {
            InstallAddress entity = base.Model2Entity(model);


            /***************************************** 下面处理自定义转换 *****************************************/



            return entity;
        }
        #endregion

        #region 读数据(集合)
        /// <summary>
        /// 读数据，从Entity对象转成Model对象
        /// </summary>
        /// <param name="entities">Entity对象</param>
        /// <returns></returns>
        public override List<InstallAddressModel> Entity2Model(IEnumerable<InstallAddress> entities)
        {
            return entities.Select(x => Entity2Model(x)).ToList();
        }
        #endregion

        #region 写数据(集合)
        /// <summary>
        /// 写数据，从Model对象转换成Entity对象
        /// </summary>
        /// <param name="models">ViewModel对象</param>
        /// <returns></returns>
        public override List<InstallAddress> Model2Entity(IEnumerable<InstallAddressModel> models)
        {
            return models.Select(x => Model2Entity(x)).ToList();
        }
        #endregion

        #region 搜索条件转换
        /// <summary>
        /// 搜索条件转换，从FilterModel对象转换成Where对象
        /// </summary>
        /// <param name="filterModel">FilterModel对象</param>
        /// <returns></returns>
        public override Expressionable<InstallAddress> FilterModel2Where(InstallAddressFilterModel filterModel)
        {
            var where = base.FilterModel2Where(filterModel);

            //处理数据权限
            if (_currentUser.CurrentUserInfo.LoginUserType == LoginUserType.集团)
            {
                if (_currentUser.CurrentUserInfo.LoginUserRoleDepartment.Item1 == false)
                {
                    var pkIDs = _ILoginUserDataPermission.GetTablePkIDs(DataDepartment_Type.安装地点, null, _currentUser.CurrentUserInfo.LoginUserRoleDepartment.Item2.ToArray());
                    where = where.And(x => pkIDs.Contains(x.InstallAddressID));
                }
            }

            //SearchInput
            if (!string.IsNullOrEmpty(filterModel._SearchInput))
            { }

            //地点
            if (!string.IsNullOrEmpty(filterModel._AddressInfo))
            {
                where = where.And(x => x.AddressInfo.Contains(filterModel._AddressInfo));
                //where = where.And(x => x.AddressInfo == filterModel._AddressInfo);
            }

            ////创建人
            //if (filterModel._CreateUserID.HasValue)
            //{
            //    where = where.And(x => x.CreateUserID == filterModel._CreateUserID.Value);
            //}

            ////修改人
            //if (filterModel._UpdateUserID.HasValue)
            //{
            //    where = where.And(x => x.UpdateUserID == filterModel._UpdateUserID.Value);
            //}


            return where;
        }
        #endregion
    }
}
